package com.qdlc.p2p.manage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qd.p2p.sina.pay.response.QueryHostingDepositParamResponse;
import com.qd.p2p.sina.pay.response.QueryHostingTradeResponse;
import com.qd.p2p.sina.pay.response.QueryHostingWithdrawResponse;
import com.qd.p2p.sina.service.SinaPayService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.TppQueryModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * Created by D on 2016/2/2.
 */
public class ManageTppQueryAction extends BaseAction<TppQueryModel> implements ModelDriven<TppQueryModel> {

    @Resource
    private SinaPayService sinaPayService;

    @Resource
    private UserService userService;

    private Map<String, Object> data;

    /**
     * 托管交易查询页面
     */
    @Action(value = "/modules/tppQuery/hostingTrade")
    public String hostingTrade() {
        return "hostingTrade";
    }

    /**
     * 托管交易查询
     */
    @Action(value = "/modules/tppQuery/queryHostingTrade")
    public void queryHostingTrade() throws Exception {
        data = new HashMap<String, Object>();
        QueryHostingTradeResponse queryHostingTradeResponse = null;
        String tppUserCustId="";
        if(StringUtil.isNotBlank(model.getUserName())) {
            User user = userService.getUserByUserName(model.getUserName());
            if (user == null)
                return;
            tppUserCustId= UserUtils.getUserTppCustId(user.getUserId());
        }
        if (StringUtil.isNotBlank(model.getOutTradeNo())) {
            queryHostingTradeResponse = sinaPayService.queryHostingTrade(model.getOutTradeNo());
        } else if (StringUtil.isNotBlank(tppUserCustId)) {
            queryHostingTradeResponse = sinaPayService.queryHostingTrade(tppUserCustId, DateUtil.dateStr3(DateUtil.valueOf(model.getStartTime())), DateUtil.dateStr3(DateUtil.valueOf(model.getEndTime()+ " 23:59:59")), String.valueOf(model.getPage()),String.valueOf(model.getRows()));
        }
        if (queryHostingTradeResponse != null) {
            String[] args = queryHostingTradeResponse.getTradeList().split("\\|");
            List<TppQueryModel> list = new ArrayList<TppQueryModel>();
            if (StringUtil.isNotBlank(args[0])) {
                for (String s : args) {
                    String[] arg = s.split("\\^");
                    TppQueryModel tppQueryModel = new TppQueryModel();
                    switch (arg.length){
                        case 6:tppQueryModel.setUpdateTime(arg[5]);
                        case 5:tppQueryModel.setCreateTime(arg[4]);
                        case 4:tppQueryModel.setStatus(arg[3]);
                        case 3:tppQueryModel.setAmount(Double.parseDouble(arg[2] + ""));
                        case 2:tppQueryModel.setSummary(arg[1]);
                        case 1:tppQueryModel.setOutTradeNo(arg[0]);
                    }
                    list.add(tppQueryModel);
                }
            } else{
                TppQueryModel tppQueryModel=new TppQueryModel(queryHostingTradeResponse.getResponseMessage());
                list.add(tppQueryModel);
            }
            data.put("total", queryHostingTradeResponse.getTotalItem());
            data.put("rows", list);
        }
        printWebJson(getStringOfJpaObj(data));
    }

    /**
     * 托管充值查询页面
     */
    @Action(value = "/modules/tppQuery/hostingDeposit")
    public String hostingDeposit() {
        return "hostingDeposit";
    }

    /**
     * 托管充值查询
     */
    @Action(value = "/modules/tppQuery/queryHostingDeposit")
    public void queryHostingDeposit() throws Exception {
        data = new HashMap<String, Object>();
        QueryHostingDepositParamResponse queryHostingDepositParamResponse = null;
        if(StringUtil.isBlank(model.getUserName()))
            return;
        User user=userService.getUserByUserName(model.getUserName());
        if(user==null)
            return ;
        String tppUserCustId=UserUtils.getUserTppCustId(user.getUserId());
        if(StringUtil.isBlank(tppUserCustId))
            return;
        if (StringUtil.isNotBlank(model.getOutTradeNo())) {
            queryHostingDepositParamResponse = sinaPayService.queryHostingDepositParam(tppUserCustId, model.getOutTradeNo());
        } else if (StringUtil.isNotBlank(model.getStartTime())) {
            queryHostingDepositParamResponse = sinaPayService.queryHostingDepositParam(tppUserCustId, DateUtil.dateStr3(DateUtil.valueOf(model.getStartTime())), DateUtil.dateStr3(DateUtil.valueOf(model.getEndTime()+ " 23:59:59")), model.getPage(),model.getRows());
        }
        if (queryHostingDepositParamResponse != null) {
            String[] args = queryHostingDepositParamResponse.getDetailList().split("\\|");
            List<TppQueryModel> list = new ArrayList<TppQueryModel>();
            if (StringUtil.isNotBlank(args[0])) {
                for (String s : args) {
                    String[] arg = s.split("\\^");
                    TppQueryModel tppQueryModel = new TppQueryModel();
                    switch (arg.length){
                        case 5:tppQueryModel.setUpdateTime(arg[4]);
                        case 4:tppQueryModel.setCreateTime(arg[3]);
                        case 3:tppQueryModel.setStatus(arg[2]);
                        case 2:tppQueryModel.setAmount(Double.parseDouble(arg[1] + ""));
                        case 1:tppQueryModel.setOutTradeNo(arg[0]);
                    }
                    list.add(tppQueryModel);
                }
            } else{
                TppQueryModel tppQueryModel=new TppQueryModel(queryHostingDepositParamResponse.getResponseMessage());
                list.add(tppQueryModel);
            }
            data.put("total", queryHostingDepositParamResponse.getTotalList() / 20 + 1);
            data.put("rows", list);
        }
        printWebJson(getStringOfJpaObj(data));
    }

    /**
     * 托管提现查询页面
     */
    @Action(value = "/modules/tppQuery/hostingWithdraw")
    public String hostingWithdraw() {
        return "hostingWithdraw";
    }

    /**
     * 托管提现查询
     */
    @Action(value = "/modules/tppQuery/queryHostingWithdraw")
    public void queryHostingWithdraw() throws Exception {
        data = new HashMap<String, Object>();
        QueryHostingWithdrawResponse queryHostingWithdrawResponse = null;

        if(StringUtil.isBlank(model.getUserName()))
            return;
        User user=userService.getUserByUserName(model.getUserName());
        if(user==null)
            return ;
        String tppUserCustId=UserUtils.getUserTppCustId(user.getUserId());
        if(StringUtil.isBlank(tppUserCustId))
            return;

        if (StringUtil.isNotBlank(model.getOutTradeNo())) {
            queryHostingWithdrawResponse = sinaPayService.queryHostingWithdraw(tppUserCustId, model.getOutTradeNo());
        } else if (StringUtil.isNotBlank(model.getStartTime())) {
            queryHostingWithdrawResponse = sinaPayService.queryHostingWithdraw(tppUserCustId, DateUtil.dateStr3(DateUtil.valueOf(model.getStartTime())), DateUtil.dateStr3(DateUtil.valueOf(model.getEndTime()+ " 23:59:59")), String.valueOf(model.getPage()),String.valueOf(model.getRows()));
        }
        if (queryHostingWithdrawResponse != null) {
            String[] args = queryHostingWithdrawResponse.getWithdrawList().split("\\|");
            List<TppQueryModel> list = new ArrayList<TppQueryModel>();
            if (StringUtil.isNotBlank(args[0])) {
                for (String s : args) {
                    String[] arg = s.split("\\^");
                    TppQueryModel tppQueryModel = new TppQueryModel();
                    switch (arg.length){
                        case 5:tppQueryModel.setUpdateTime(arg[4]);
                        case 4:tppQueryModel.setCreateTime(arg[3]);
                        case 3:tppQueryModel.setStatus(arg[2]);
                        case 2:tppQueryModel.setAmount(Double.parseDouble(arg[1] + ""));
                        case 1:tppQueryModel.setOutTradeNo(arg[0]);
                    }
                    list.add(tppQueryModel);
                }
            } else{
                TppQueryModel tppQueryModel=new TppQueryModel(queryHostingWithdrawResponse.getResponseMessage());
                list.add(tppQueryModel);
            }
            data.put("total", queryHostingWithdrawResponse.getTotalItem() / 20 + 1);
            data.put("rows", list);
        }
        printWebJson(getStringOfJpaObj(data));
    }
}
